module(..., package.seeall) -- 使得文件中的函数在何处都可调用
require "nvm"
require "earthquakeWaring"
require "mqttOutMsg"

nvm.init("config.lua", true)
local clientNo = nvm.get("info")[1]

local slen, offllineData = string.len, ""

-- 预警设备参数设置-身份判别
local function containClientsOrNot(clients)
    do
        local containResult = false
        for i = 1, #clients do
            if clients[i] == nvm.get("info")[1] then
                containResult = true
            end
        end
        log.info("containResult", containResult)
        return containResult
    end
end

-- 设备重启完成版本升级
function deviceRestartUpgrade(tjsondata)
    if tjsondata["clients"] then
        if containClientsOrNot(tjsondata["clients"]) then
            offllineData = "{" .. "clientNo:'" .. clientNo .. "',msg:'0'" .. "}"
            offllineData = crypto.aes_encrypt("ECB", "PKCS5", offllineData,
                                              "keson-123abcdefg")
            offllineData =
                crypto.base64_encode(offllineData, slen(offllineData))
            log.info("offllineData:", offllineData)
            mqttOutMsg.insertMsg("online", offllineData, 0)
            sys.waitUntil("ONLINE_OR_OFFLINE_SEND_DATA")
            sys.restart("重启设备，完成升级")
        end
    end
end
